package com.yisen.dasan.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yisen.dasan.util.ResultData;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/form")
public class ImportController {
    @PostMapping("/import")
    public ResultData imports(@RequestBody JSONObject req){
        System.out.println(req);
        JSONArray jsonArray=req.getJSONArray("data");
        System.out.println(jsonArray);
        JSONObject result=new JSONObject();
        Map<String,Object> head = new HashMap<String,Object>();// 封装head属性
        List<JSONObject> text = new ArrayList<>();// 封装 text
        JSONObject jsonObject=jsonArray.getJSONObject(0);
        Map map=jsonObject;
        int count=0;
        for (Object key : map.keySet()){
            String a="a";
            a=a+count;
            count=count+1;
            head.put(a,String.valueOf(key));
        }
        for (int i=0;i<jsonArray.size();i++){
            JSONObject json=jsonArray.getJSONObject(i);
            Map map1=json;
            int c=0;
            JSONObject jsonObject1=new JSONObject();
            for (Object key : map1.keySet()){
                String b="a";
                b=b+c;
                c=c+1;
                jsonObject1.put(b,map1.get(key));
            }
            text.add(jsonObject1);
        }
        result.put("head",head);
        result.put("text",text);
        System.out.println(result);
        return new ResultData(true ,200,"成功！",result);
    }
}
